<template>
  <div class="cont animate__fadeInLeft">
    <el-button icon="el-icon-s-promotion" @click="addS">添加</el-button>
    <div class="div">
      <!-- 内容 -->
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column label="日期" width="150">
          <template slot-scope="scope">
            {{ scope.row.createTime | formatTimer }}
          </template>
        </el-table-column>

        <!-- ----------------------------------------- -->
        <el-table-column prop="id" label="公告ID" width="120">
        </el-table-column>
        <el-table-column prop="title" label="公告标题" width="210">
        </el-table-column>
        <el-table-column prop="" label="公告内容" width="102">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="small"
              @click="drawers(scope.row.content)"
              >查看内容</el-button
            >
          </template>
        </el-table-column>
        <el-table-column prop="author" label="公告作者" width="120">
        </el-table-column>

        <el-table-column label="状态" width="150">
          <template slot-scope="scope">
            <el-button type="primary" size="small" @click="byOk(scope.row, 1)">
              启用
            </el-button>
            <el-button type="danger" size="small" @click="byOk(scope.row, 0)">
              停用
            </el-button>
          </template>
        </el-table-column>

        <el-table-column prop="" label="公告状态" width="121" >
          <template slot-scope="scope">
            {{ scope.row.status | byNoOk }}
          </template>
        </el-table-column>

         <el-table-column  label="操作" width="150" >
          <template slot-scope="scope">
            <el-button type="primary" size="small" @click="upDates(scope.row)">
              修改
            </el-button>
            <el-button type="danger" size="small" @click="del(scope.row.id)">
              删除
            </el-button>
          </template>
        </el-table-column>

      </el-table>

      <!-- 分页 -->
      <el-pagination
        background
        layout="prev, pager, next"
        :total="Pagination.total"
        style="margin: 20px 20px 20px 0px"
        @current-change="currentchange"
        :page-size="Pagination.pageSize"
      >
      </el-pagination>
      <!-- 公告内容 -->
      <el-drawer
        title="我是标题"
        :visible.sync="drawer"
        :with-header="false"
        size="500px"
        style="font-size: 15px"
      >
        <div v-html="paperCont"></div>
      </el-drawer>
      <!-- 添加公告内容 -->
      <el-drawer
        title="公告内容"
        :visible.sync="drawerTwo"
        :with-header="false"
        size="500px"
        style="font-size: 15px"
      >
        <div>
          <el-form ref="form" :model="form" style="margin: 40px">
            <el-form-item>
              <el-input placeholder="公告标题" v-model="form.title"></el-input>
            </el-form-item>

            <el-form-item>
              <el-input placeholder="作者姓名" v-model="form.author"></el-input>
            </el-form-item>

            <el-form-item>
              <el-select v-model="form.status" placeholder="1为启用，2为停用">
                <el-option label="1" value="1"></el-option>
                <el-option label="0" value="0"></el-option>
              </el-select>
            </el-form-item>

            <div label="内容">
              <el-card style="height: 400px; width: 420px">
                <quill-editor
                  v-model="form.content"
                  ref="myQuillEditor"
                  style="height: 500px"
                >
                </quill-editor>
              </el-card>
            </div>
            <el-form-item style="margin-top: 30px; margin-left: 0px">
              <el-button type="primary" @click="onSubmit(1)">确定</el-button>
              <el-button>取消</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-drawer>
    </div>
  </div>
</template>

<script>
import { quillEditor } from "vue-quill-editor";
import "quill/dist/quill.core.css";
import "quill/dist/quill.snow.css";
import "quill/dist/quill.bubble.css";

export default {
  name: "FuncFormsEdit",
  components: {
    quillEditor,
  },
  data() {
    return {
      editorOption: "",
      z: "",
      paperCont: "",
      drawer: false,
      drawerTwo: false,
      //分页
      Pagination: {
        page: 1,
        pageSize: 7,
        total: 2,
      },
      users: {
        name: "",
        password: "",
      },
      updateid: "",
      tableData: [],
      form: {
        title: "",
        content: "",
        author: "",
        status: "",
      },
    };
  },
  mounted() {
    this.currentchange();
  },
  filters: {
    byNoOk(vule) {
      if (vule == 1) return "√ 已启用";
      else return "× 未启用";
    },
  },
  methods: {
    upDates(row) {
      this.form.title = row.title;
      this.form.content = row.content;
      this.form.author = row.author;
      this.form.status = row.status;

      this.drawerTwo = true;
      this.updateid = row.id;
      this.z = 1;
    },
    onSubmit() {
      let cot = this.form;
      let id = this.updateid;
      if (this.z == 0) {
        this.$http
          .post("/notice/add", {
            ...cot,
          })
          .then((res) => {
            this.currentchange();
          });
      } else if ((this.z = 1)) {
        this.$http
          .post("/notice/update", {
            id,
            ...cot,
          })
          .then((res) => {
            this.currentchange();
          });
      }
      this.drawerTwo = false;
    },
    drawers(z) {
      this.paperCont = z;
      this.drawer = true;
    },
    byOk(row, z) {
      console.log(row);
      console.log(z);
      this.$http
        .post("/notice/update", {
          id: row.id,
          title: row.title,
          content: row.content,
          author: row.author,
          status: z,
        })
        .then((res) => {
          console.log(res);
          if (res.code == 0) {
            if (z == 1) {
              this.currentchange();
              this.$notify({
                title: "成功",
                message: "审核成功通过！",
                type: "success",
                duration: 700,
              });
            } else {
              this.currentchange();
              this.$notify({
                title: "驳回",
                message: "成功驳回内容！",
                type: "warning",
                duration: 700,
              });
            }
          }
        });
    },
    del(d) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$http.post("/notice/del", { id: d }).then((res) => {
            // console.log(res);
            this.currentchange();
            this.$message({
              type: "success",
              message: res.msg,
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //添加.....
    addS() {
      this.drawerTwo = true;
      this.z = 0;
    },
    currentchange(z) {
      if (z) {
        this.Pagination.page = z;
      }
      this.$http
        .get("/notice/", {
          ...this.Pagination,
        })
        .then((res) => {
          console.log(res);
          this.tableData = res.data;
          this.Pagination.page = Number(res.page);
          this.Pagination.pageSize = Number(res.pageSize);
          this.Pagination.total = Number(res.total);
        });
    },
    login() {
      this.$http
        .post("/login/", {
          ...this.users,
        })
        .then((res) => {
          localStorage.setItem("token_z", res.info.token);
        });
    },
  },
};
</script>

<style scoped>
.cont {
padding: 20px;
/* animation:logo_go 1s;
-webkit-animation:logo_go1 1s; */
  animation: fadeInLeft; /* referring directly to the animation's @keyframe declaration */
  animation-duration: 0.9s;
}
.div {
  /* border:1px solid red; */
  margin-top: 10px;
  width: 1123px;

}
.from {
  width: 100px;
}

</style>